var co = require('co');
var seqModule = require('../../../routes/createModules');
var seq = new seqModule();
var util = require('../../../common/util');

//met一周直播
var liveController = {
    getLiveWxData: function (req, res) {
        var firstDay = util.getFirstDay();
        var lastDay = util.getLastDay();
        var nowTime = util.getNowTime();
        var memberSql = "";
        if (req.query.memberId != undefined) {
            memberSql += " and member_id = '" + req.query.memberId + "'";
        }
        var liveSql = "select case when DATE_FORMAT(start_time,'%Y-%m-%d') <= '" + util.getNowDate() + "' then 1 else 0 end as isNowDay,live.*,(select count(*) from lm_signup_log where live_id = live.id " + memberSql + ") as isSign, (select count(*) from lm_signup_log where live_id = live.id ) as signCount from lm_live as live where (live.sort = 101 or live.sort = 102)" +
            " and live.start_time between '" + firstDay + "' and '" + lastDay + "' and live.end_time > '" + nowTime + "'";
		liveSql += " order by start_time ASC";
        seq.sequelize.query(liveSql).spread(function (result) {
            res.json({ 'success': true, 'msg': '查询成功！', "result": result });
        }).catch(function (err) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(err);
        })

    },
    getLiveWxDHData: function (req, res) {//获取大会的数据
        var nowTime = util.getNowTime();
        var memberSql = "";
        if (req.query.memberId != undefined) {
            memberSql += " and member_id = '" + req.query.memberId + "'";
        }
        var liveSql = "select case when DATE_FORMAT(start_time,'%Y-%m-%d') <= '" + util.getNowDate() + "' then 1 else 0 end as isNowDay,live.*,(select count(*) from lm_signup_log where live_id = live.id) as signCount,(select count(*) from lm_signup_log where live_id = live.id " + memberSql + ") as isSign from lm_live as live where live.sort = 101 " +
            " and live.end_time > '" + nowTime + "'";
        if (req.query.pTitle != undefined) {
            liveSql += " and (live.title like '%" + req.query.pTitle + "%' or live.expert_name like '%" + req.query.pTitle + "%')";
        }
        liveSql += " order by start_time ASC";
        seq.sequelize.query(liveSql).spread(function (result) {
            res.json({ 'success': true, 'msg': '查询成功！', "result": result });
        }).catch(function (err) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(err);
        })

    },
    getLiveWxJTData: function (req, res) {//获取大讲堂的数据
        var nowTime = util.getNowTime();
        var memberSql = "";
        if (req.query.memberId != undefined) {
            memberSql += " and member_id = '" + req.query.memberId + "'";
        }
        var liveSql = "select case when DATE_FORMAT(start_time,'%Y-%m-%d') <= '" + util.getNowDate() + "' then 1 else 0 end as isNowDay,live.*,(select count(*) from lm_signup_log where live_id = live.id) as signCount,(select count(*) from lm_signup_log where live_id = live.id " + memberSql + ") as isSign from lm_live as live where live.sort = 102 " +
            " and live.end_time > '" + nowTime + "'";
        if (req.query.pTitle != undefined) {
            liveSql += " and (live.title like '%" + req.query.pTitle + "%' or live.expert_name like '%" + req.query.pTitle + "%') ";
        }
        liveSql += " order by start_time ASC";
        seq.sequelize.query(liveSql).spread(function (result) {
            res.json({ 'success': true, 'msg': '查询成功！', "result": result });
        }).catch(function (err) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(err);
        })

    }
}
module.exports = liveController;